﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class MerOperSetController : Controller
    {
        //
        // GET: /MerOperSet/

        ZHHQSERVICE.Service1Client service_User = new ZHHQSERVICE.Service1Client();
        ServiceReference1.Service1Client service_Order = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            try
            {
                List<Oper> New_Oper = new List<Oper>();
                // List<Model.View_OperUser> Oper = new List<Model.View_OperUser>();
                string result = service_User.GetAllOperUser("", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Oper m = new Oper();
                            m.id = item["OperID"].ToString();
                            m.name = item["OperNum"].ToString();
                            New_Oper.Add(m);
                        }
                    }
                    ViewBag.ll = New_Oper;
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
            return View();
        }

        public ActionResult getgrid(int OperID, JqGridParam jqgridparam)
        {
            try
            {
                Page<MerOper> page = new Page<MerOper>();//分页类定义
                List<MerOper> list = new List<MerOper>();
                #region 根据操作员获取所有商户
                List<Model.Order_Mer_T> list_Mer = new List<Model.Order_Mer_T>();
                string ResMer = service_Order.GetMer(OperID, "", 1);
                JObject searchMer = JObject.Parse(ResMer);//将服务返回的结果进行优化
                string bsMer = searchMer["BS"].ToString();//获取返回结果是否正常标识
                if (bsMer == "1")//返回结果正常
                {
                    IList<JToken> jieguoMer = searchMer["rows"].Children().ToList();//解析返回的结果
                    if (jieguoMer.Count > 0)
                    {
                        foreach (JToken item in jieguoMer)
                        {
                            Model.Order_Mer_T m = new Model.Order_Mer_T();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            list_Mer.Add(m);
                        }
                    }
                }
                #endregion
                string result = service_Order.GetOrder_Mer("", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            List<MerOper> list_Dishes = new List<MerOper>();
                            MerOper m = new MerOper();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            #region 查找有权限的商户并选中
                            foreach (Model.Order_Mer_T Mer in list_Mer)
                            {
                                if (Mer.MerID == m.MerID)
                                {
                                    m.IsCheck = true;
                                    break;
                                }
                            }
                            #endregion
                            list.Add(m);
                        }
                        if (list.Count > 0)
                        {
                            jqgridparam.records = list.Count;//查询的数据总数
                            var data = new
                            {
                                total = list.Count.ToString(),
                                rows = page.FindListPage(list, ref jqgridparam)
                            };
                            return Json(data);
                        }
                        else
                        {
                            var data = new
                            {
                                total = "0",
                                rows = list
                            };
                            return Json(data);
                        }
                    }
                    else
                    {
                        var data = new
                        {
                            total = "0",
                            rows = list
                        };
                        return Json(data);
                    }
                }
                else
                {
                    return Json(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        public ActionResult SaveMerOper(int OperID, string MerID)
        {
            try
            {
                string result = "";
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
            
                MerID = MerID.Remove(0,1);
                #region 转换传入的商品信息
                Model.Order_Oper_T New_OrderOper = new Model.Order_Oper_T();
                New_OrderOper.OperID = OperID;
                New_OrderOper.MerID = MerID;
                #endregion
                string a = New_OrderOper.ToJson();
                result = service_Order.SetMerOperID(OperID, New_OrderOper.ToJson(), GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content("保存成功");
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }

        #region 获取操作员所管理的商户
        public ActionResult getMers(int OperID, JqGridParam jqgridparam)
        { 
            try
            {
                List<Model.Order_Mer_T> list_Mer = new List<Model.Order_Mer_T>();
                string ResMer = service_Order.GetMer(OperID, "", 1);
                JObject searchMer = JObject.Parse(ResMer);//将服务返回的结果进行优化
                string bsMer = searchMer["BS"].ToString();//获取返回结果是否正常标识
                if (bsMer == "1")
                {
                    IList<JToken> jieguoMer = searchMer["rows"].Children().ToList();//解析返回的结果
                    list_Mer = ParseClass<Model.Order_Mer_T>.JsonList(jieguoMer);//解析返回的结果\

                    var data = new
                    {
                        total = searchMer["total"].ToString(),
                        rows = list_Mer
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searchMer["total"].ToString(),
                        rows = list_Mer
                    };
                    return Json(data);
                }

            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        /// <summary>
        /// 操作员信息定义
        /// </summary>
        public class Oper
        {
            public string id { get; set; }
            public string name { get; set; }
            public string Mer { get; set; }
        }

        /// <summary>
        /// 商户操作员权限
        /// </summary>
        public class MerOper
        {
            public bool IsCheck { get; set; }
            public string MerID { get; set; }
            public string MerName { get; set; }
            public string MerTxt { get; set; }
            public string MerAdr { get; set; }
            public string MerCall { get; set; }
        }
    }
}
